package com.gs.userservice.config;

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 * @ProjectName: springcloudapollo20190719
 * @Package: com.gs.userservice.config
 * @Description: java类作用描述
 * @Author: guoshuai
 * @CreateDate: 2019/7/22 15:29
 * @UpdateUser: guoshuai
 * @UpdateDate: 2019/7/22 15:29
 * @UpdateRemark: The modified content
 * @Version: 1.0
 **/
@Configuration
public class ApolloConfigChanged implements ApplicationContextAware {

    private Logger logger = LoggerFactory.getLogger(this.getClass());



    private ApplicationContext applicationContext;

    @ApolloConfigChangeListener
    private void someChangeHandler(ConfigChangeEvent changeEvent) {
        for (String key : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(key);
            logger.info("Found change - {}", change.toString());
        }

        // 更新相应的bean的属性值，主要是存在@ConfigurationProperties注解的bean
        this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
      this.applicationContext = applicationContext ;
    }
}
